package com.gzdh.dishrecognition.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dhkj.web.utils.MyObserverRes;
import com.dhkj.web.utils.RequestUtils;
import com.gzdh.dishrecognition.MainActivity;
import com.gzdh.dishrecognition.R;
import com.gzdh.dishrecognition.config.SysConstConfig;
import com.gzdh.dishrecognition.ui.adapter.report.OrderDetailsAdapter;
import com.gzdh.dishrecognition.util.print.PrintUtil;

import net.posprinter.posprinterface.TaskCallback;

import java.io.IOException;
import java.math.BigDecimal;

public class OrderDetailsDialog extends Dialog {

    private Context mContext;


    private JSONArray jsonArray;

    private TextView cardNoTv;

    private TextView nameTv;
    private TextView orderTv;
    private TextView czrqTv;
    TextView refundBut;
    private OrderDetailsAdapter orderDetailsAdapter;

    private RefundFina mRefundFina;

    public void setRefundFina(RefundFina mRefundFina) {
        this.mRefundFina = mRefundFina;
    }

    public interface RefundFina {
        void onRefund();
    }

    @SuppressLint("SetTextI18n")
    public void setJsonArray(JSONArray jsonArray) {
        this.jsonArray = jsonArray;
        cardNoTv.setText("工号：" + jsonArray.getJSONObject(0).getString("cardno"));
        nameTv.setText("姓名：" + jsonArray.getJSONObject(0).getString("kehu"));
        orderTv.setText("订单号：" + jsonArray.getJSONObject(0).getString("zfserialnumber"));
        czrqTv.setText("下单时间：" + jsonArray.getJSONObject(0).getString("czrq"));
        orderDetailsAdapter.setJsonArray(jsonArray);
        if (1 == jsonArray.getJSONObject(0).getInteger("cs") || !SysConstConfig.getInstance().isUseRefundBtn()) {
            refundBut.setVisibility(View.INVISIBLE);
        }
    }

    public OrderDetailsDialog(@NonNull Context context) {
        super(context, R.style.CustomDialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_order_details);
        mContext = this.getContext();
        cardNoTv = findViewById(R.id.cardNoTv);
        nameTv = findViewById(R.id.nameTv);
        orderTv = findViewById(R.id.orderTv);
        czrqTv = findViewById(R.id.czrqTv);
        RecyclerView foodsRv = findViewById(R.id.foodsRv);

        orderDetailsAdapter = new OrderDetailsAdapter(mContext);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        foodsRv.setLayoutManager(layoutManager);
        foodsRv.setAdapter(orderDetailsAdapter);
        findViewById(R.id.bdPrintBut).setOnClickListener(view -> {
            prints();
        });


        refundBut = findViewById(R.id.refundBut);
        refundBut.setOnClickListener(view -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orders", jsonArray.getJSONObject(0).getString("zfserialnumber"));
            RequestUtils.refundProduct(mContext, jsonObject.toJSONString(), new MyObserverRes<JSONObject>(mContext) {
                @Override
                public void onSuccess(JSONObject result) {
                    System.out.println("退款成功");
                    printTks();
                    if (mRefundFina != null) {
                        mRefundFina.onRefund();
                        dismiss();
                    }
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();

                }
            });
        });

    }

    public void printTks() {
        try {
            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText("膳食订餐系统");
            pUtil.printLine();
            pUtil.printLargeText("退款");
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("机器号：", jsonArray.getJSONObject(0).getString("posnum"));
            pUtil.printLine();
            pUtil.printTwoColumn("订单号:", jsonArray.getJSONObject(0).getString("zfserialnumber"));
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", jsonArray.getJSONObject(0).getString("czrq"));
            pUtil.printLine();
            // 分隔线
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printThreeColumn("商品", "数量", "小计");
            pUtil.printLine();
            BigDecimal price = new BigDecimal(0);
            for (int i = 0; i < jsonArray.size(); i++) {
                pUtil.printThreeColumn(jsonArray.getJSONObject(i).getString("hname"),
                        jsonArray.getJSONObject(i).getString("sl"),
                        jsonArray.getJSONObject(i).getString("je"));
                price = price.add(new BigDecimal(jsonArray.getJSONObject(i).getString("je")));
            }
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", price.toPlainString());
            pUtil.printLine();
            String sc = jsonArray.getJSONObject(0).getString("sc");
            if ("2".equals(sc)) {
                pUtil.printTwoColumn("消费方式:", "现金");
                pUtil.printLine();
            } else if ("3".equals(sc)) {
                pUtil.printTwoColumn("消费方式:", "微信");
                pUtil.printLine();
            }
            pUtil.printTwoColumn("支付状态:", "已退款");
            pUtil.printLine();
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printLargeText("小票仅限当日当餐有效");
            pUtil.printLine(4);
            pUtil.printQZ();
            if (MainActivity.ISCONNECT) {
                MainActivity.myBinder.Write(pUtil.getOutbyte(), new TaskCallback() {
                    @Override
                    public void OnSucceed() {
                    }

                    @Override
                    public void OnFailed() {
                    }
                });
            } else {
                Toast.makeText(mContext, "打印机尚未连接，请检查", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void prints() {
        try {
            PrintUtil pUtil = new PrintUtil();
            // 店铺名 居中 放大
            pUtil.printAlignment(1);
            pUtil.printLargeText("膳食订餐系统--(补打)");
            pUtil.printLine();
            pUtil.printAlignment(0);
            pUtil.printLine();
            pUtil.printTwoColumn("机器号：", jsonArray.getJSONObject(0).getString("posnum"));
            pUtil.printLine();
            pUtil.printTwoColumn("订单号:", jsonArray.getJSONObject(0).getString("zfserialnumber"));
            pUtil.printLine();
            pUtil.printTwoColumn("下单时间:", jsonArray.getJSONObject(0).getString("czrq"));
            pUtil.printLine();
            // 分隔线
            pUtil.printDashLine();
            pUtil.printLine();
            //打印商品列表
            pUtil.printThreeColumn("商品", "数量", "小计");
            pUtil.printLine();
            BigDecimal price = new BigDecimal(0);
            for (int i = 0; i < jsonArray.size(); i++) {
                pUtil.printThreeColumn(jsonArray.getJSONObject(i).getString("hname"),
                        jsonArray.getJSONObject(i).getString("sl"),
                        jsonArray.getJSONObject(i).getString("je"));
                price = price.add(new BigDecimal(jsonArray.getJSONObject(i).getString("je")));
            }
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printTwoColumn("合计金额:", price.toPlainString());
            pUtil.printLine();
            String sc = jsonArray.getJSONObject(0).getString("sc");
            if ("2".equals(sc)) {
                pUtil.printTwoColumn("消费方式:", "现金");
                pUtil.printLine();
            } else if ("3".equals(sc)) {
                pUtil.printTwoColumn("消费方式:", "微信");
                pUtil.printLine();
            }
            if (jsonArray.getJSONObject(0).getInteger("cs") == 0) {
                pUtil.printTwoColumn("支付状态:", "已支付");
                pUtil.printLine();
            } else {
                pUtil.printTwoColumn("支付状态:", "已退款");
                pUtil.printLine();
            }
            pUtil.printDashLine();
            pUtil.printLine();
            pUtil.printLargeText("小票仅限当日当餐有效");
            pUtil.printLine(4);
            pUtil.printQZ();
            if (MainActivity.ISCONNECT) {
                MainActivity.myBinder.Write(pUtil.getOutbyte(), new TaskCallback() {
                    @Override
                    public void OnSucceed() {
                    }

                    @Override
                    public void OnFailed() {
                    }
                });
            } else {
                Toast.makeText(mContext, "打印机尚未连接，请检查", Toast.LENGTH_SHORT).show();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
